<?php

namespace app\admin\controller;

use app\model\OpenClass as OC;
class OpenClass extends AdminBase
{

    // CREATE TABLE `lcxx_open_class` (
    //     `id` int NOT NULL AUTO_INCREMENT,
    //     `title` varchar(255) NOT NULL DEFAULT '',
    //     `create_user` varchar(255) NOT NULL DEFAULT '' COMMENT '创建人',
    //     `start_time` int NOT NULL DEFAULT '0',
    //     `end_time` int NOT NULL DEFAULT '0',
    //     `is_free` int NOT NULL DEFAULT '1' COMMENT '是否免费 1 免费 2 收费',
    //     `vertical_img` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
    //     `horizontal_img` varchar(255) NOT NULL DEFAULT '',
    //     `tag` varchar(255) NOT NULL DEFAULT '',
    //     `course_desc` text,
    //     `course_imgs` text,
    //     `participants` text,
    //     `resources` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,
    //     `participants_number` int NOT NULL DEFAULT '0',
    //     `status` int NOT NULL DEFAULT '0',
    //     PRIMARY KEY (`id`)
    //   ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;


    private $ptitle = 'title/s';
    private $pcreate_user = 'create_user/s';
    private $pstart_time = 'start_time/s';
    private $pend_time = 'end_time/s';
    private $pis_free = 'is_free/d';
    private $pvertical_img = 'vertical_img/s';
    private $phorizontal_img = 'horizontal_img/s';
    private $ptag = 'tag/s';
    private $pcourse_desc = 'course_desc/s';
    private $pcourse_imgs = 'course_imgs';
    private $pparticipants = 'participants/s';
    private $presources = 'resources/s';
    private $pstatus = 'status/d';
    

    public function index(string $do = '')
    {
        if ($do == 'json') {
            $where = [];
            $kw  = $this->request->param('kw');
            if ($kw) $where[] = ['name','like','%'.$kw.'%'];
            $list = (new OC())->listQuery($where);
            $list = $list->toArray();
            foreach ($list['data'] as  &$v) {
                $v['start_time'] = date('Y-m-d H:i:s',$v['start_time']);
                $v['end_time'] = date('Y-m-d H:i:s',$v['end_time']);
                $v['course_imgs']  = json_decode($v['course_imgs'],true);
            }
            return $this->returnMsg($list,code: 0);
        }
        $this->assign([ 'limit'=>10]); 
        return $this->fetch();
        
    }


    public function bedit(string $do = '')
    {
        $d = $this->only($do ? ['@token'=>'','id/d/ID参数错误','av','af'] : ['@token'=>'','id/d/ID参数错误',$this->ptitle,$this-> pcreate_user,$this->pstart_time,$this->pend_time, $this->pis_free, $this->pvertical_img, $this->phorizontal_img, $this->ptag, $this->pcourse_desc, $this->pcourse_imgs, $this->pparticipants, $this->presources, $this->pstatus,$this->pis_free]);
        $id = $d['id'];
        $Myobj = OC::get("id = $id");
        if(!$Myobj) return $this->returnMsg("数据不存在");
        if ($do == 'up') {
            $value = $d['av'];
            $field = $d['af'];
            if(!in_array($field,['title','create_user','start_time','end_time','is_free', 'vertical_img', 'horizontal_img', 'tag', 'course_desc', 'course_imgs', 'participants', 'resources', 'status','is_free'])) return $this->returnMsg("参数错误");  
            if($field == 'img') $this->only([$this->pvertical_img, $this->phorizontal_img]);
            if($Myobj->save([$field=>$value])){
                return $this->returnMsg("设置成功", code: 1);
            }
        }else{
            if (!empty($d["pvertical_img"])) $d["pvertical_img"] = $d["pvertical_img"][0];
            if (!empty($d["phorizontal_img"])) $d["phorizontal_img"] = $d["phorizontal_img"][0];
            if (!empty($d["course_imgs"])) $d["course_imgs"] = json_encode($d["course_imgs"]);
            $participants = explode("\n", $d["participants"]);
            $d["participants_number"] = count($participants);
            $d['start_time'] = strtotime($d['start_time']);
            $d['end_time'] = strtotime($d['end_time']);
            if($Myobj->save($d)){
                return $this->returnMsg("编辑成功", code: 1);
            }
        }
    }
    

    public function badd()
    {
        $d = $this->only(['@token'=>'id/d/ID参数错误',$this->ptitle,$this-> pcreate_user,$this->pstart_time,$this->pend_time, $this->pis_free, $this->pvertical_img, $this->phorizontal_img, $this->ptag, $this->pcourse_desc, $this->pcourse_imgs, $this->pparticipants, $this->presources, $this->pstatus,$this->pis_free]);
        if (!empty($d["pvertical_img"])) $d["pvertical_img"] = $d["pvertical_img"][0];
        if (!empty($d["phorizontal_img"])) $d["phorizontal_img"] = $d["phorizontal_img"][0];
        if (!empty($d["course_imgs"])) $d["course_imgs"] = json_encode($d["course_imgs"]);
        $participants = explode("\n", $d["participants"]);
        $d["participants_number"] = count($participants);
        $d['start_time'] = strtotime($d['start_time']);
        $d['end_time'] = strtotime($d['end_time']);
        if((new OC())->save($d)){
            return $this->returnMsg("添加成功", code: 1);
        }else{
            return $this->returnMsg("添加失败");
        }
    }

    //del
    public function bdel()
    {
        $d = $this->only(['@token'=>'','id/d/ID参数错误']);
        $id = $d['id'];
        $Myobj = OC::get("id = $id");
        if(!$Myobj) return $this->returnMsg("数据不存在");
        if($Myobj->delete()){
            return $this->returnMsg("删除成功", code: 1);
        }
    }    
}

